<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 2.面试题 ：版本号比较

        // 写一函数，函数名为compareVersion    给你两个版本号比较大小  满足如下条件

        // compareVersion("2.1.5" ，“2.0.3”） 返回  1

        // compareVersion("2" ，“3.1.0”） 返回 - 1

        // compareVersion("2.0.0" ，“2.0”）返回  0



        function compareVersion(str1, str2) {
            let arr1 = str1.split('.')
            let arr2 = str2.split('.')

            // if(arr1.length === arr2.length){
            //     return 1
            // }else if(arr1.length - arr2.length === 2 || arr1.length - arr2.length === -2){
            //     return -1
            // }else if(arr1.length - arr2.length === 1 || arr1.length - arr2.length === -1){
            //     return 0
            // }

            console.log(22222);

            for (let i = 0; true; i++) {
                if ((arr1[i] === undefined) && (arr2[i] === undefined)) {
                    return 0
                }
                else if ((arr1[i] > arr2[i]) || (arr2[i] === undefined && arr1[i] !== '0')) {
                    return 1
                } else if ((arr1[i] < arr2[i]) || (arr1[i] === undefined && arr2[i] !== '0')) {
                    return -1
                }
                
            }
        }
        
        console.log(compareVersion("2" , "3.1.0"))
       console.log(compareVersion("2.1.5" , "2.0.3"))
       console.log(compareVersion("2.0.0.1" , "2.0"))

</script>
</body>

</html>